package com.burgeon.backend.Rest.User;
/**
 * 获取用户信息
 */


import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetUserALL extends RESTBase {
    private final static String RANGE = "50";//分页数量

    private String ip;
    private String account;
    private String password;

    public GetUserALL(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public HashMap getData(int start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(start), url);
        HashMap rows = new HashMap();
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)){
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        rows.put("count", count);
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<User> list = new ArrayList<User>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            User user = new User();
            user.setName(array.getString(0));
            user.setPhone(array.getString(1));
            user.setIntegral(array.getString(2));
            user.setId(array.getString(3));
            user.setType(array.getString(4));
            user.setStore(array.getString(5));
            user.setCustomer(array.getString(6));
            user.setEmployee(array.getString(7));
            user.setBirthday(array.getString(8));
            user.setAge(array.getString(9));


            list.add(user);
        }
        rows.put("data", list);

        return rows;
    }

    private MultiValueMap Data(int y) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("VIPNAME");
        columns.add("CARDNO");
        columns.add("INTEGRAL");
        columns.add("ID");
        columns.add("'C_VIPTYPE_ID;NAME'");//等级
        columns.add("'C_STORE_ID;NAME'");//店仓
        columns.add("OPENCARDDATE");//开卡时间
        columns.add("'HR_EMPLOYEE_ID;NAME'");//开卡人
        columns.add("BIRTHDAY");
        columns.add("AGE");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        params.put("range", RANGE);
        params.put("count", "true");
        int r = Integer.valueOf(RANGE);
        params.put("start", String.valueOf(y * r));
        HashMap orderby = new HashMap();


        orderby.put("column", "OPENCARDDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);


        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetUserALL g = new GetUserALL("39.104.52.2:55321", "zhaohong@bestone.net", "666888");
        try {
            System.err.println(g.getData(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
